/**
 * 
 */
package com.gmail.hh.flickrj.sample.android.images;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Gallery.LayoutParams;

import com.gmail.hh.flickrj.sample.android.R;
import com.gmail.hh.flickrj.sample.android.data.PhotoData;
import com.gmail.hh.flickrj.sample.android.data.PhotolistData;
import com.gmail.hh.flickrj.sample.android.images.ImageUtils.DownloadedDrawable;
import com.gmail.hh.flickrj.sample.android.tasks.LoadImageTask;
import com.gmail.hh.flickrj.sample.android.tasks.LoadImageTask.IImageLoadedListener;
import com.gmail.hh.flickrj.sample.android.tasks.LoadUserForContactsTask.IUserLoadedListener;
import com.googlecode.flickrjandroid.contacts.Contact;
import com.googlecode.flickrjandroid.people.User;
import com.googlecode.flickrjandroid.photos.Photo;

/**
 * @author Toby Yu(yu yang..226@gmail.com)
 *
 */
@SuppressLint("NewApi")
public class LazyAdapterContacts extends BaseAdapter implements
													IImageLoadedListener{

	private static final Logger logger = LoggerFactory.getLogger(LazyAdapterContacts.class);
	
	// each Class using this Adapter  must implement this interface
	private IRemoveContactListener mCallbackRemoveContact;
	private ListFragment mCallbackListFragment;
	public interface IRemoveContactListener {
		public void onRemoveContact(int position);
	}
	
    private Activity activity;
    private User[] mContactsUser;
    private static LayoutInflater inflater=null;
    
    @SuppressLint("NewApi")
	public LazyAdapterContacts(ListFragment callbackListFragment,
    				IRemoveContactListener callbackRemoveContact,
    				Activity a, Collection<User> contactsUser) {
    	mCallbackListFragment = callbackListFragment;
    	mCallbackRemoveContact = callbackRemoveContact;
        activity = a;
        if (contactsUser == null)  {
        	mContactsUser = new User[0];
        }
        else  {
        	mContactsUser =  contactsUser.toArray( new User[contactsUser.size()] );
        }
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
	@Override
    public int getCount() {
        return mContactsUser.length;
    }

	@Override
	   public User getItem(int position) {
		return mContactsUser[position];
    }

	@Override
	   public long getItemId(int position) {
        return position;
    }
    
	@Override
	   public View getView( final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        User user = this.getItem(position);
		vi =null; // when reusing view there is a problem with multiple use f imageView
        if(vi == null)  {
            vi = inflater.inflate(R.layout.contacts_row, null);
        }
        
        ImageView imageContact=(ImageView)vi.findViewById(R.id.imageContact);
        TextView usernameContact =(TextView)vi.findViewById(R.id.usernameContact);
        TextView realnameContact =(TextView)vi.findViewById(R.id.realnameContact);
        ImageButton btnRemoveContacts =(ImageButton)vi.findViewById(R.id.btnRemoveContacts);
        usernameContact.setText( user.getUsername() );
        realnameContact.setText( user.getRealName() );
        btnRemoveContacts.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mCallbackRemoveContact!=null) {
					mCallbackRemoveContact.onRemoveContact(position);
				}
			}
		});
        // set a click Listener to the complete item, because stanard-onListItem doesnt work
        vi.setOnClickListener(new OnClickListener() {
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				if (mCallbackListFragment!=null) {
					mCallbackListFragment.onListItemClick(null, null, position, getItemId(position) );
				}
			}
		});
        if (position==0) {
        	btnRemoveContacts.setVisibility(View.GONE);
        }
        if (imageContact != null && user.getBuddyIconUrl()!=null) {
        	logger.error("username: " + usernameContact.getText().toString() );
        	logger.error("url: " + user.getBuddyIconUrl() );
        	logger.error("image: " + imageContact.toString() );
        	boolean showProgress = false;
        	long delayMSEC = 0;
        	PhotoData photoData=null;
        	LoadImageTask task = new LoadImageTask(null, delayMSEC, photoData, imageContact, showProgress, null);
            Drawable drawable = new DownloadedDrawable(task);
            imageContact.setImageDrawable(drawable);
            task.execute( user.getBuddyIconUrl() );
        }
		
        return vi;
    }

	// callback method after loadImage asynchron
	@Override
	public void onImageLoaded(PhotoData photoData) {
		// no action necessary nothing after loading image
	}
	

	
}
